import Vue from 'vue'
import Router from 'vue-router'

// import MainView from '@/layouts/MainView'
import MainView from '@/layouts/tabs/TabsView'
import RouteView from '@/layouts/RouteView'
import PageView from '@/layouts/PageView'

/* NavigationDuplicated 错误处理 */
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

Vue.use(Router)

// export default router
// export default router
export const constantRoutes = [
  {
    path: '/login',
    name: '登录页',
    component: () => import('@/views/login')
  }
]

export const rootRoute = {
  path: '/',
  component: MainView,
  hidden: true,
  children: [
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/WorkPlace'),
      meta: {
        icon: 'dashboard'
      }
    }
  ]
}

export const extraRoutes = [
  {
    path: '/system',
    name: '系统页面',
    component: PageView,
    hidden: true,
    children: [
      {
        path: '/system/profile',
        name: '个人中心',
        component: () => import('@/views/user/profile')
      }
    ]
  },
  {
    path: '/403',
    name: 'Exp403',
    component: () => import('@/views/exception/403')
  },
  {
    path: '/500',
    name: 'Exp500',
    component: () => import('@/views/exception/500')
  },
  {
    path: '/404',
    name: 'Exp404',
    component: () => import('@/views/exception/404')
  }
]

export const notFoundRoute = {
  path: '*', // 此处需特别注意至于最底部
  redirect: '/404'
}

export const devRoutes = [
  {
    path: '/demo',
    name: 'I18n Demo',
    meta: {
      icon: 'file-ppt'
    },
    component: () => import('@/views/demo')
  },
  {
    path: '/form',
    name: '表单页',
    component: PageView,
    meta: {
      icon: 'form'
    },
    children: [
      {
        path: '/form/basic',
        name: '基础表单',
        component: () => import('@/views/form/BasicForm'),
        icon: 'none'
      }
    ]
  },
  {
    path: '/demo2',
    name: '展示页',
    component: PageView,
    meta: {
      icon: 'table'
    },
    children: [
      {
        path: '/demo2/index',
        name: '展示页详情',
        component: () => import('@/views/demo2')
      }
    ]
  },
  {
    path: '/exception',
    name: '异常页',
    meta: {
      icon: 'warning'
    },
    component: RouteView,
    children: [
      {
        path: '/404',
        name: 'Exp404 ',
        component: () => import('@/views/exception/404')
      },
      {
        path: '/403',
        name: 'Exp403 ',
        component: () => import('@/views/exception/403')
      },
      {
        path: '/500',
        name: 'Exp500 ',
        component: () => import('@/views/exception/500')
      }
    ]
  }
]

export const buildAsyncRoutes = (routes) => {
  const res = []
  routes.forEach((route) => {
    const tmp = { ...route }
    // tmp.component = !tmp.component ? Layout : asyncComponentMap[tmp.component]
    const component = tmp.component.trim().replace('@/', '')
    tmp.component = () => import(`@/${component}`)
    tmp.meta = { ...tmp.meta, title: tmp.title, icon: tmp.icon, permissionCodes: tmp.permissionCodes }
    if (tmp.children) {
      tmp.children = buildAsyncRoutes(tmp.children)
    }
    res.push(tmp)
  })

  return res
}

const createRouter = () =>
  new Router({
    mode: 'history', // require service support
    base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

const router = createRouter()

export default router
